//
//  JSModelChooseViewController.m
//  JigSaw
//
//  Created by Slim Wang on 15/1/13.
//  Copyright (c) 2015年 LITB. All rights reserved.
//

#import "JSModelChooseViewController.h"
#import "JSPreviewManager.h"
#import "JSDeviceInfo.h"
#import "UIColor+Help.h"
#import "UIImage+Help.h"
#import "JSModelChooseCell.h"
#import <QuartzCore/QuartzCore.h>
#import "JSModelManager.h"
#define BASE_CELL_WIDTH 140
#define BASE_CELL_HEIGHT 210
#define BASE_CELL_MIN_SPACE 14
#define BASE_LINE_MIN_SPACE 14

@interface JSModelChooseViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property(nonatomic, weak)IBOutlet UICollectionView* collectionView;
@property(nonatomic, strong)NSMutableArray* modelArray;
@property(nonatomic, assign)float adaptScale;
@property(nonatomic, weak)IBOutlet UIButton* backBtn;

@end

@implementation JSModelChooseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHex:0xe8e8e8];
    self.collectionView.backgroundColor = [UIColor colorWithHex:0xe8e8e8];
    self.adaptScale = [JSDeviceInfo getScreenScale];
    self.collectionView.layer.masksToBounds = NO;
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [[JSPreviewManager sharedInstance] resetPreView];
    [[JSPreviewManager sharedInstance] hiddenPreView];
}

- (IBAction)backButtonClicked:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [JSModelManager sharedInstance].stencils.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [JSModelManager sharedInstance].selectedModelIndex = indexPath.row;
    [self performSegueWithIdentifier:@"showPhotoGroupChoose" sender:self];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    JSModelChooseCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"modelCell" forIndexPath:indexPath];
    cell.layer.shadowOpacity = 0.1;
    cell.layer.shadowColor = [[UIColor colorWithHex:0x000000] CGColor];
    cell.layer.shadowOffset = CGSizeMake(0, 1.5);
    cell.layer.masksToBounds = NO;
    cell.layer.shadowPath = [[UIBezierPath bezierPathWithRect:cell.bounds] CGPath];
    cell.layer.shadowRadius = 0;    
    cell.imageView.image = [[JSModelManager sharedInstance] getThumbnailImage:indexPath.row];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(BASE_CELL_WIDTH*self.adaptScale, BASE_CELL_HEIGHT
                      *self.adaptScale);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return BASE_LINE_MIN_SPACE*self.adaptScale;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return BASE_CELL_MIN_SPACE*self.adaptScale;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.backBtn.alpha = 0;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.backBtn.alpha = 1;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if(!decelerate) self.backBtn.alpha = 1;
}

- (void)viewDidAppear:(BOOL)animated
{
    self.backBtn.alpha = 1;
}
@end
